/*
  this link list program can be used to form table for compression
   which is Header File can Be used by others program to use
   for Building Table
	   -occurence of char in file and number of time it does

   *it will sort the file according to the number of time it occures
   *display the table(sorted or unsorted)

  @ ISSC By
		   vishwajeet dusane

  What this
	 -build table i.e. occurence of each char in the file
	 -sort them according to the number of char in file in descending order
*/
#include<stdio.h>
#include<process.h>
#include<iostream.h>
#include<conio.h>
#include<alloc.h>

struct node
{
  int ch;
  int counter;
  struct node *next;
};

class BuildTable
{
public:
int search(struct node **,int );
void display(struct node *);
void create(struct node **,int);
void SortTable(struct node **);
int getPosition(struct node *,int);
int getCharacter(struct node **,int);
void replaceNumber(char fname[30]);
};

 void BuildTable:: create(struct node **head,int ch)
 {
   struct node *temp=*head;

	if(*head==NULL)
	  {
	   *head=(struct node *)malloc(sizeof(struct node));
	  (*head)->next=NULL;
	(*head)->ch=ch;
	(*head)->counter=1;
	   }
	  else
	  {
	  temp=*head;
	  while(temp->next!=NULL)
	  temp=temp->next;
	  temp->next=(struct node *)malloc(sizeof(struct node));

	  temp=temp->next;
	  temp->next=NULL;
	 temp->ch=ch;
	 temp->counter=1;
	   }
 }
 int BuildTable::getCharacter(struct node **head,int pos)
 {
   struct node *temp=*head;

	if(*head==NULL)
	  {
	  return NULL;
	  }
	  else
	  {
		 temp=*head;
			 while(temp!=NULL)
			 {
				 pos--;
				 if(pos==0)
				  return temp->ch;
				temp=temp->next;
			 }
	  }
  return NULL;



 }
 void BuildTable::display(struct node *temp)
 {

	while(temp!=NULL)
	{
	 printf("%c\t%d \n",temp->ch,temp->counter);
	 temp=temp->next;
	 }
	 getch();
 }
void BuildTable::replaceNumber(char fname[30])
{
   FILE *fp,*fp1;
   int ch;
	fp=fopen(fname,"r");
	fp1=fopen("c:\Huffnum.txt","w");
	if(fp==NULL||fp1==NULL)
	{
	printf("File Not Found");
	getch();
	exit(0);
	}
	while((ch=fgetc(fp))!=EOF)
	 fprintf(fp1,"%d",ch);
   fcloseall();
}
int BuildTable:: search(struct node **head,int ch)
{
   struct node *temp=*head;

	if(*head==NULL)
	  {
	  return 1;
	  }
	  else
	  {
	 temp=*head;
	  while(temp!=NULL)
	  {
		if(temp->ch==ch)
		{
		 temp->counter++;
		 return 0;
		}
	  temp=temp->next;
	  }
	   }
  return 1;
}

int BuildTable::getPosition(struct node *temp,int ch)
{
	int pos=0;
	if(temp==NULL)
	  {
	  return 0;
	  }
	  else
	  {
	  while(temp!=NULL)
	   {
	   pos++;
		if(temp->ch==ch)
		{
		 return pos;
		}
	  temp=temp->next;
	  }
	   }
  return 0;
}

void BuildTable:: SortTable(struct node **head)
{
	struct node *temp,*temp1;
	int tempCounter,tempCh;
	int i,j;

  if(*head==NULL)
	return;
  else
  {
	temp=*head;
   while(temp!=NULL)
  {
	 temp1=*head;
	while(temp1!=NULL)
	{
	   if( ( temp1 -> counter ) < ( temp -> counter ) )
		{
		 tempCounter = temp1 -> counter;
		 temp1 -> counter = temp -> counter;
		 temp -> counter = tempCounter;
		 tempCh = temp1 -> ch;
		 temp1 -> ch = temp -> ch;
		 temp -> ch = tempCh;
		}
		 temp1 = temp1 -> next;
	}
	temp=temp->next;
  }
}
}
